package de.hs_zigr.inf.iib08gruppe5powerfish.it.control;

import java.io.IOException;
import javax.swing.DefaultListModel;
import org.xmldb.api.base.XMLDBException;

import de.hs_zigr.inf.iib08gruppe5powerfish.it.boundary.GUIQueryProg;
import de.hs_zigr.inf.iib08gruppe5powerfish.it.entity.Connection;
import de.hs_zigr.inf.iib08gruppe5powerfish.it.entity.Quiry;

/**
 * big control for QueryProg
 */
public class CtrlQueryProg {

	/** to set driver and collection for eXist driver */
	private CtrlDBDriver_Collection ctrlDBDriverCollection = new CtrlDBDriver_Collection();
	/** to load / store driver or collection for eXist db */
	private Connection connection = new Connection();
	/** to load / store dlm */
	private Quiry query;
	/** stores XQueries for lstQueries */
	private DefaultListModel dlm;
	/** stores XQueries for lstQueries */
	private DefaultListModel dlmHistory;
	/** to load / store dlmHistory */
	private Quiry queryHistory;
	/** go get access to the eXist-DB */
	private CtrlEXistDriver eXistDriver;
	/** to add a date to a XQuery */
	private CtrlDateAdder dateAdder;

	public void start(){
		GUIQueryProg main = new GUIQueryProg();
		main.start();
	}
	
	/** set DB connection for using eXist */
	public void setDBConnection() throws NullPointerException,
			InstantiationException, IllegalAccessException,
			ClassNotFoundException, XMLDBException, IOException {
		eXistDriver = ctrlDBDriverCollection.setDBConnetion();
	}

	/** gets eXist db - driver */
	public String getConLoaderDriver() throws IOException,
			ClassNotFoundException {
		return connection.getDriver();
	}

	/** gets eXist db - collection */
	public String getConLoaderCollection() throws IOException,
			ClassNotFoundException {
		return connection.getCollection();
	}

	/** stores driver string of an eXist db */
	public void setConStorerDriver(String strDriverL) throws IOException {
		connection.setDriver(strDriverL);
	}

	/** stores collection string of an eXist db */
	public void setConStorerCollection(String strCollectionL)
			throws IOException {
		connection.setCollection(strCollectionL);
	}

	/**
	 * loads queries from file. initializes dlm with stored XQueries as
	 * templates for lstQueries
	 * 
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	public DefaultListModel loadStoredQueries() throws IOException,
			ClassNotFoundException {
		query = new Quiry();
		dlm = query.loadQueries("QueryFile.dat");
		return dlm;
	}

	/**
	 * loads history queries from file. initializes dlmHistory with stored
	 * XQueries history as templates for lstQueriesHistory
	 * 
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	public DefaultListModel loadStoredQueriesHistory() throws IOException,
			ClassNotFoundException {
		queryHistory = new Quiry();
		dlmHistory = queryHistory.loadQueries("QueryFileHistory.dat");
		return dlmHistory;
	}

	/**
	 * stores written XQuery
	 * 
	 * @throws IOException
	 */
	public void storeQuery2File(String strQuiry) throws IOException {
		query = new Quiry();
		dlm.addElement(strQuiry);
		query.storeQueries(dlm, "QueryFile.dat");
	}

	/**
	 * stores applied XQuery
	 * 
	 * @throws IOException
	 */
	public void storeQuery2FileHistory(String strQuiry) throws IOException {
		queryHistory = new Quiry();
		dateAdder = new CtrlDateAdder();
		dlmHistory.addElement(dateAdder.addDate(strQuiry));
		queryHistory.storeQueries(dlmHistory, "QueryFileHistory.dat");
	}

	/**
	 * applies XQuery
	 * 
	 * @throws XMLDBException
	 */
	public String runScript(String strXQuery) throws XMLDBException {
		String strResultLoc = "";
		strResultLoc = eXistDriver.getResultSet(strXQuery);
		return strResultLoc;
	}
}